import cn.com.elite.Util;
import cn.com.kh.KHUtil;
import cn.com.kh.getProperty;
import cn.com.kh.hst.ThreeDesUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONObject;

import java.time.Instant;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @projectName: ngsapitask-hst
 * @package: cn.com.kh
 * @className: Tao_Dev
 * @author: LiJT
 * @description: TODO
 * @date: 2023-01-12 16:50
 * @version: 1.0
 */
public class sessionCheck {
    private static final Log log = LogFactory.getLog(sessionCheck.class);

    public static void main(String[] args) throws Exception {

        log.debug(checkUserID("4576423aa82eeb3f1fb02ace894ee162a6cd2342e9302d8cc2da4034a5c8630a9babd295a6f5bf1ce05514aa6e08b58b", "106687767", "WHATSAPP"));

    }


    public static String checkUserID(String token, String hsNo, String source) {

        String userid = "";
        if (source.equalsIgnoreCase("WHATSAPP")) {
            log.warn("WhatsApp渠道不支持校验Token");
            userid = hsNo;
        } else {
            if (!KHUtil.isNull(token) && !KHUtil.isNull(hsNo)) {
                if (checkTokenInvoid(token, hsNo)) {
                    userid = hsNo;
                }
            } else {
                log.warn("进线入参链接Token和hsNo不满足非空校验,默认访客");
            }
        }
        return userid;
    }

    public static boolean checkTokenInvoid(String token, String hsNo) {
        boolean re_Flag = false;
        try {

            HashMap<String, String> paramHashMap = new HashMap<String, String>();
            HashMap<String, String> paramHashMap1 = new HashMap<String, String>();
            String appid = getProperty.get("appid");
            String appsecret = getProperty.get("appsecret");

            //获取时间戳
            String sign_time = String.valueOf(Instant.now().getEpochSecond());
            log.debug("sign_time：" + sign_time);

            paramHashMap.put("token", token);
            paramHashMap.put("hsNo", hsNo);
            paramHashMap.put("sign_time", sign_time);
            log.debug("paramHashMap：" + paramHashMap);

            paramHashMap1.put("token", token);
            paramHashMap1.put("hsNo", hsNo);
            paramHashMap1.put("sign_time", sign_time);
            log.debug("paramHashMap1：" + paramHashMap1);

            //自然排序 升序参数
            String signfront = ThreeDesUtil.sortMap(paramHashMap, appid, appsecret);
            log.debug("sign加密MD5前：" + signfront);
            String signafter = Util.getMD5Str(signfront);
            log.debug("sign加密MD5后：" + signafter);

            paramHashMap1.put("client_key", appid);
            paramHashMap1.put("sign", signafter);

            String injson = ThreeDesUtil.SplicingMap(paramHashMap1, "&");
            log.debug("发送地址&拼接后：" + injson);

            String doURL = getProperty.get("HSTURL");
            doURL = doURL + "/session/check";

            Map<String, String> headMap = new HashMap<String, String>();
            headMap.put("Content-Type", "application/x-www-form-urlencoded");
            String getResult = Util.doHttpPostUseHead(doURL, injson, "UTF-8", 10000, 0, headMap);
            log.debug("getResult:" + getResult);

            //解析返回信息
            JSONObject jsonObject = new JSONObject(getResult);
            String code = GetJV(jsonObject, "code");
            String result = GetJV(jsonObject, "result");
            log.debug("code:" + code + " result:" + result);

            if (Objects.equals(result, "true")) {
                re_Flag = true;
            }
        } catch (Exception e) {

        }

        return re_Flag;
    }

    // 解析Json值
    public static String GetJV(JSONObject inJson, String getKey) {
        String Rev_Value = null;
        try {
            Rev_Value = inJson.getString(getKey);
        } catch (Exception e) {
            try {
                Rev_Value = String.valueOf(inJson.getInt(getKey));
            } catch (Exception e1) {
                try {
                    Rev_Value = String.valueOf(inJson.getJSONArray(getKey));
                } catch (Exception e2) {
                    try {
                        Rev_Value = String.valueOf(inJson.getJSONObject(getKey));
                    } catch (Exception e3) {

                        try {
                            Rev_Value = String.valueOf(inJson.getBoolean(getKey));
                        } catch (Exception e4) {

                            try {
                                Rev_Value = String.valueOf(inJson.getDouble(getKey));
                            } catch (Exception e5) {
                                Rev_Value = "";
                            }
                        }
                    }
                }
            }
        } finally {
            if (Rev_Value == null || Rev_Value.equals("null") || Rev_Value.length() <= 0) {
                Rev_Value = "";
            } else {
            }
        }
        return Rev_Value;
    }
}